import { _decorator, Component, Node, Sprite } from 'cc';
import { BaseView } from '../../../../scripts/libs/rxjs/cc3/BaseView';
import { ReactiveProperty } from '../../../../scripts/libs/rxjs/cc3/ReactiveProperty';
import { rm } from '../../../../scripts/managers/ResourceManager';
import { subgame002 } from '../../Tools';
import { BindNodeTouch } from '../../../../scripts/libs/rxjs/cc3/BindNode';
import { game002 } from '../../Game002Manager';
const { ccclass, property } = _decorator;

@ccclass('Card')
export class Card extends BaseView {

    mainTextureResPath = 'ab_subgame002/texture-0/poker';

    cardId = ReactiveProperty.Create(null);
    cardIdOri = -1;

    @property(Sprite)
    mainSprite: Sprite = null;

    enableTouch = true;
    willDestroy = false;

    useObserves() {
        let list = [
            this.cardId.subscribe(this.cardIdChanged.bind(this)),
        ];
        list.push(BindNodeTouch(this.node).subscribe(_ => {
            game002.cardNodeTouchCommand.execute({ card: this, eventTouch: _ });
        }));
        return list;
    }

    cardIdChanged() {
        this.log('cardIdChanged', this.cardId.value);
        if (this.cardId.value == null || this.cardId.value < 0) {
            this.mainSprite.spriteFrame = null;
        } else {
            this.mainSprite.spriteFrame = rm.spriteAtlas[this.mainTextureResPath].getSpriteFrame(subgame002.tools.cardIdToTexName(this.cardId.value));
        }
    }

}

